<?php

/**
 * This class represents the "Context" in the Strategy Pattern. It will read the database settings and will decide which
 * strategy we have to use.
 * @author Daniel Rivera
 */

require 'DBConfig.php';
require_once 'ConnectionStrategy.php';
require_once 'MySQLConnection.php';
require_once 'PostgreConnection.php';


 
class DBContext {
	
	private $strategy;
	
	/**
	 * Method to create a database connection depending on the engine configured.
	 */
	public function createConnection() {
		switch (DBENGINE) {
			case 'mysql':
				$this->strategy = new MySQLConnection();
				break;
			case 'postgresql':
				//do nothing at all yet
				break;
			case 'sqlite':
				//do nothing at all yet
				break;
		}
		$this->strategy->connect();
	}
	
	/**
	 * The schema variable corresponds to an array where instances of columns are stored in 
	 * another array. So this method will read that structure to create an JSON output format
	 * @author Daniel Rivera
	 */
	private function getSchemaJSON($schema, $tablenames) {
		$jsonfile = '{ "tables": {';
		$i = 0;
		while ($i < sizeof($schema)) {
			$table = $schema[$i];
			$jsonfile .= '"' . $tablenames[$i] . '": { "columns": { ';
			$j = 0;
			$primarykey = ''; //we store the name of the primary key here
			while ($j < sizeof($table)) {
				$column = $table[$j]; //it's an instance of Column
				if ($column->columnkey) $primarykey = $column->columnname;
				$jsonfile .= '"' . $column->columnname . '": "'.$column->columntype . '"';
				if ($j < sizeof($table) - 1) {
					$jsonfile .= ', ';
				}
				$j++;
			}
			$jsonfile .= ' , "primarykey": "' . $primarykey . '"';
			$jsonfile .= '} ';
			$i++;
		}
		$jsonfile .= '} }';
		echo $jsonfile;
	}
	
	/**
	 * This method does the same thing that the previous one but in XML format.
	 * @author Daniel Rivera
	 */
	private function getSchemaXML($schema, $tablenames) {
		echo "It doesn't work yet";
		die ('not implemented yet');
		$jsonfile = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		echo $jsonfile;
		$jsonfile .= '<!DOCTYPE schema PUBLIC "-//phpdumper//DTD DocBook V3.1//EN">';
		$jsonfile .= "\n<tables>";
		
		$i = 0;
		while ($i < sizeof($schema)) {
			$table = $schema[$i];
			$jsonfile .= "\n\t<" .$tablenames[$i] . ">\n" . "\n\t\t<columns>";
			$j = 0;
			$primarykey = ''; //we store the name of the primary key here
			while ($j < sizeof($table)) {
				$column = $table[$j]; //it's an instance of Column
				if ($column->columnkey) $primarykey = $column->columnname;
				$jsonfile .= "\n\t\t\t<column name=\"" . $column->columnname . " type=\"".$column->columntype . "\"/>";
				$j++;
			}
			$jsonfile .= "\n\t\t</columns>";
			$jsonfile .= "\n\t\t<primarykey name=\"" . $primarykey . ">";
			$i++;
		}
		$jsonfile .= "\n\t</" . $tablenames[$i] . ">";
		$jsonfile .= "\n</tables>";
		echo $jsonfile;
	}
	
	private function getDataJSON($tablename, $rows) {
		echo 'getDataJSON';
		$outputstring = '{ "' . $tablename .'": { ';
		for ($i = 0; $i < sizeof($rows); $i++) {
			$outputstring .= '"row": "' . $rows[$i] . '"';
			if ($i < sizeof($rows)-1) {
				$outputstring .= ', ';
			}
		}
		$outputstring .= '}';
		echo 'output: '.$outputstring;
		return $outputstring;
	}
	
	private function getDataXML($tablename, $rows) {
		die('not implemented yet');
	}
	
	/**
	 * Method called to obtain the schema of the configured database, indicating the output file
	 * format.
	 */
	public function getSchema($output) {
		$auxlist =  $this->strategy->getTables();
		$schema = array();
		$i = 0;
		while ($i < sizeof($auxlist)) {
			array_push($schema, $this->strategy->getColumns($auxlist[$i]));
			$i++;
		}
		$schemaString = '';
		switch ($output) {
			case 'json':
				$schemaString = $this->getSchemaJSON($schema, $auxlist);
				break;
			case 'xml':
				$schemaString = $this->getSchemaXML($schema, $auxlist);
				break;
		}
		
		return $schemaString;
	}
	
	/**
	 * Method called to obtain data in an specific table with an indicated format.
	 */
	public function getDataFromTable($output, $tablename) {
		echo 'table = '.$tablename;
		$auxlist = $this->strategy->getRowsFromTable($tablename);
		var_dump($auxlist);
		$dataString = '';
		switch ($output) {
			case 'json':
				$dataString = $this->getDataJSON($tablename,$auxlist);
				break;
			case 'xml':
				$dataString = $this->getDataXML($tablename,$auxlist);
				break;
		}
		return $dataString;
	}
	
}


?>
